package xin.marcher.module.trace.api.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.exception.ProductBizException;
import xin.marcher.module.trace.api.ProductTraceApi;
import xin.marcher.module.trace.domain.dto.TraceDataDTO;
import xin.marcher.module.trace.domain.request.QueryTraceDataPageRequest;
import xin.marcher.module.trace.service.TraceDataService;

/**
 * 溯源数据
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = ProductTraceApi.class, retries = 0)
public class ProductTraceApiImpl implements ProductTraceApi {

    @Autowired
    private TraceDataService traceDataService;

    /**
     * 通用的数据溯源接口
     *
     * @param request
     */
    @Override
    public JsonResult<PageResult<TraceDataDTO>> listTraceDataPage(QueryTraceDataPageRequest request) {
        try {
            PageResult<TraceDataDTO> resultDTO = traceDataService.listTraceDataPage(request);
            return JsonResult.buildSuccess(resultDTO);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
